package com.kitty.record.kittyio.nio.one;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;
import java.util.Scanner;

/**
 * @Description
 * @Author SHEN
 * @Date 2021/2/18
 */
public class Client {

    public static void main(String[] args) throws Exception {
        //1.获取通道
        SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1",9999));

        //2.切换非阻塞模式
        sChannel.configureBlocking(false);

        //3.分配指定大小的缓冲区
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

        //4.发送数据给服务端
        Scanner scanner = new Scanner(System.in);

        while (scanner.hasNext()){

            String str = scanner.nextLine();
            byteBuffer.put(str.getBytes());
            byteBuffer.flip();
            sChannel.write(byteBuffer);
            byteBuffer.clear();

        }

        //5.关闭通道
        sChannel.close();




    }

}
